<template>
  <div
    class="page_body"
    v-loading.fullscreen.lock="loadingKey"
    :element-loading-text="loadingText"
  >
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item class="breadcrumb_active_item"
            >赛事管理</el-breadcrumb-item
          >
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <div class="match_manager_content">
        <!-- 按钮栏 -->
        <el-row>
          <el-col :span="24">
            <div class="match_manager_top">
              <el-button
                size="mini"
                type="primary"
                @click="addFn()"
                v-if="userType == 0"
                >新增</el-button
              >
              <el-input
                v-model="searchKey"
                placeholder="请输入关键词"
                size="mini"
                style="width: 200px; margin-left: 10px"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                style="margin-left: 5px"
                @click="searchFn()"
                >搜索</el-button
              >
            </div>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <div v-if="matchListData != undefined && matchListData.length > 0">
          <el-row>
            <el-col :span="24">
              <el-table
                :data="matchListData"
                border
                size="mini"
                :cell-style="cellStyleFn"
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                  align="center"
                >
                </el-table-column>

                <el-table-column
                  label="赛事名称名称"
                  align="center"
                  min-width="250"
                >
                  <template slot-scope="scope">
                    <el-link
                      type="primary"
                      @click="detailFn(scope.$index, scope.row)"
                      >{{ scope.row.title }}</el-link
                    >
                  </template>
                </el-table-column>

                <el-table-column
                  prop="event_date"
                  label="赛事名称时间"
                  width="180"
                  align="center"
                ></el-table-column>

                <el-table-column
                  prop="place"
                  label="地址"
                  width="250"
                  align="center"
                ></el-table-column>

                <el-table-column
                  label="审核流程"
                  min-width="180"
                  align="center"
                >
                  <template slot-scope="scope">
                    {{
                      scope.row.flow_id == null || scope.row.flow_id == ""
                        ? "未绑定"
                        : scope.row.flow_name
                    }}
                  </template>
                </el-table-column>

                <!-- <el-table-column
                  prop="created_at"
                  label="创建时间"
                  min-width="180"
                  align="center"
                ></el-table-column> -->

                <el-table-column label="报名名单" align="center" width="160">
                  <template slot-scope="scope">
                    <el-link
                      type="primary"
                      @click="rosterFn(scope.$index, scope.row)"
                      >点击查看</el-link
                    >
                  </template>
                </el-table-column>

                <el-table-column
                  prop="release_status"
                  label="发布状态"
                  width="100"
                  align="center"
                  :formatter="formatterReleaseStatusFn"
                ></el-table-column>
                <el-table-column
                  label="操作"
                  :width="userType == 0 ? '440' : '200'"
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-tooltip
                      effect="light"
                      content="部门管理"
                      placement="bottom"
                    >
                      <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-user"
                        @click="bindDepartmentFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip>

                    <el-tooltip
                      effect="light"
                      content="审核流程"
                      placement="bottom"
                    >
                      <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-setting"
                        v-if="userType == 0"
                        @click="bindWorkflowFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip>

                    <el-tooltip
                      effect="light"
                      content="权限区域管理"
                      placement="bottom"
                    >
                      <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-full-screen"
                        @click="areaManagerFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip>

                    <el-tooltip
                      effect="light"
                      content="证件种类管理"
                      placement="bottom"
                    >
                      <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-s-ticket"
                        @click="passManagerFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip>

                    <!-- <el-tooltip
                      effect="light"
                      content="报名名单"
                      placement="bottom"
                    >
                      <el-button
                        size="mini"
                        type="success"
                        icon="el-icon-document-copy"
                        @click="rosterFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip> -->

                    <el-tooltip
                      effect="light"
                      content="发布"
                      placement="bottom"
                      v-if="scope.row.release_status == 2 && userType == 0"
                    >
                      <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-upload2"
                        @click="releaseFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip>

                    <el-tooltip
                      effect="light"
                      content="下架"
                      placement="bottom"
                      v-if="scope.row.release_status == 1 && userType == 0"
                    >
                      <el-button
                        size="mini"
                        type="warning"
                        icon="el-icon-download"
                        @click="releaseFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip>
                    <el-tooltip
                      effect="light"
                      content="编辑"
                      placement="bottom"
                    >
                      <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-edit-outline"
                        v-if="userType == 0"
                        @click="editFn(scope.$index, scope.row)"
                        :disabled="scope.row.release_status == 1"
                      ></el-button>
                    </el-tooltip>

                    <el-tooltip
                      effect="light"
                      content="删除"
                      placement="bottom"
                    >
                      <el-button
                        size="mini"
                        type="danger"
                        icon="el-icon-delete"
                        v-if="userType == 0"
                        @click="deleteFn(scope.$index, scope.row)"
                      ></el-button>
                    </el-tooltip>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="match_manager_pagination">
                <el-pagination
                  @size-change="listSizeChange"
                  @current-change="listCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="pageSizes"
                  :page-size="pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                >
                </el-pagination>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-row v-else>
          <el-empty description="暂无数据"></el-empty>
        </el-row>
        <!-- 弹窗 -->
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          :close-on-click-modal="false"
          width="500px"
        >
          <el-form
            ref="dialogDetailForm"
            :model="detailFormData"
            :rules="detailFormRules"
          >
            <el-form-item
              label="选择流程"
              prop="workflowSelect"
              :label-width="formLabelWidth"
            >
              <el-select
                size="small"
                clearable
                v-model="detailFormData.workflowSelect"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in workflowSelectList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="dialogclose()">取 消</el-button>
            <el-button
              size="small"
              type="danger"
              @click="restBnt()"
              v-if="userType == 0"
              >重 置</el-button
            >
            <el-button
              size="small"
              type="primary"
              v-if="userType == 0"
              @click="dialogDetailFormSubmit()"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
    </el-row>
  </div>
</template>
<style src="../../assets/css/match/matchList.css" scoped></style>

<script>
export default {
  name: "MatchList",
  components: {},
  data() {
    return {
      userType: this.$store.state.loginUserType == "ADMINLOGINUSERCH" ? 0 : 1, // 用户类型;1. 普通; 0. 超级;
      baseAPIUrl: this.$store.state.baseAPIUrl,
      matchListData: [], // 数据列表
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50, 100], // 可选页容量
      pageSize: 10, // 当前页容量
      total: 0, // 总条数
      releaseStatusList: [
        {
          key: 2,
          value: "未发布",
        },
        {
          key: 1,
          value: "已发布",
        },
      ], // 发布状态
      searchKey: "", // 搜索关键词
      loadingKey: false,
      loadingText: "",
      dialogTitle: "", // 弹窗标题
      dialogVisible: false, // 弹窗显示
      detailFormData: {
        workflowSelect: "",
      }, // 部门弹窗数据
      detailFormRules: {
        workflowSelect: [
          { required: true, message: "请选择流程", trigger: "change" },
        ],
      },
      workflowSelectList: [], // 流程列表
      formLabelWidth: "90px",
    };
  },
  created: function () {
    this.getListDataFn();
    this.getWorkflowListDataFn();
  },
  methods: {
    // 获取列表数据
    getListDataFn: function () {
      let that = this;
      let url = "/admin/event/index";
      let data = {
        page: this.currentPage,
        perPage: this.pageSize,
        keywords: this.searchKey,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.matchListData = response.list;
            that.total = response.total_count;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 获取流程列表数据
    getWorkflowListDataFn: function () {
      let that = this;
      let url = "/admin/flow/index";
      let data = {};
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.workflowSelectList = response.list;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 新增
    addFn: function () {
      this.$router.push({
        name: "MatchFrom",
        query: {
          key: 1,
        },
      });
    },
    // 修改
    editFn: function (index, row) {
      this.$router.push({
        name: "MatchFrom",
        query: {
          id: row.id,
          key: 2,
        },
      });
    },
    // 关联部门
    bindDepartmentFn: function (index, row) {
      this.$router.push({
        name: "MatchBindDepartment",
        query: {
          id: row.id,
        },
      });
    },
    // 报名名单
    rosterFn: function (index, row) {
      const routeUrl = this.$router.resolve({
        name: "AuditList",
      });
      window.open(routeUrl.href + "?id=" + row.id, "_blank");
    },
    // 权限区域管理
    areaManagerFn: function (index, row) {
      this.$router.push({
        name: "AreaManager",
        query: {
          id: row.id,
        },
      });
    },
    // 权限区域管理
    passManagerFn: function (index, row) {
      this.$router.push({
        name: "PassManager",
        query: {
          id: row.id,
        },
      });
    },
    // 查看
    detailFn: function (index, row) {
      const routeUrl = this.$router.resolve({
        name: "MatchFrom",
      });
      window.open(routeUrl.href + "?id=" + row.id + "&key=3", "_blank");
    },
    // 发布/下架
    releaseFn: function (index, row) {
      let that = this;
      let url = "/admin/event/changeStatus";
      let data = {};
      let text = "";
      if (row.release_status == 2) {
        text = "发布";
        data = {
          id: row.id,
          status: 1,
        };
      } else {
        text = "下架";
        data = {
          id: row.id,
          status: 2,
        };
      }
      this.$confirm("确认" + text + "该赛事名称?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        that.loadingText = "处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success(text + "成功");
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },

    // 删除
    deleteFn: function (index, row) {
      let that = this;
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        let url = "/admin/event/destroy";
        let data = {
          id: row.id,
        };
        that.loadingText = "处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success("删除成功");
              let next = that.currentPage - 1;
              if (that.matchListData.length == 1) {
                that.currentPage = next >= 1 ? next : 1;
              }
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },
    // 页容量改变
    listSizeChange: function (val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getListDataFn();
    },
    // 当前页改变
    listCurrentChange: function (val) {
      this.currentPage = val;
      this.getListDataFn();
    },
    // 发布状态格式化
    formatterReleaseStatusFn: function (row) {
      let status = row.release_status;
      let text = "";
      this.releaseStatusList.forEach(function (item) {
        if (item.key == status) {
          text = item.value;
        }
      });
      return text;
    },
    // 设置单元格字体颜色
    cellStyleFn: function ({ row, columnIndex }) {
      let style = "";
      if (columnIndex == 4) {
        if (row.flow_id == null || row.flow_id == "") {
          style = "color: #F6705E";
        }
      }
      if (columnIndex == 6) {
        if (row.release_status == 1) {
          style = "color: #8ECF5F";
        } else {
          style = "color: #F6705E";
        }
      }
      return style;
    },
    // 搜索按钮
    searchFn: function () {
      this.currentPage = 1;
      this.getListDataFn();
    },
    // 绑定流程
    bindWorkflowFn: function (index, row) {
      let that = this;
      this.dialogTitle = "绑定审核流程";
      if (row.flow_id != null && row.flow_id != "") {
        this.detailFormData = {
          id: row.id,
          workflowSelect: row.flow_id,
        };
      } else {
        this.detailFormData = {
          id: row.id,
          workflowSelect: "",
        };
      }

      this.$nextTick(function () {
        that.dialogVisible = true;
        that.$refs.dialogDetailForm.clearValidate();
      });
    },
    // 弹窗确认
    dialogDetailFormSubmit: function () {
      let that = this;
      let url = "/admin/event/addFlow";
      let data = {
        event_id: this.detailFormData.id,
        flow_id: this.detailFormData.workflowSelect,
      };
      this.$refs.dialogDetailForm.validate((valid) => {
        if (!valid) {
          return false;
        } else {
          that
            .$confirm("确认提交?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
            })
            .then(() => {
              that.loadingText = "处理中...";
              that.loadingKey = true;
              that.axios
                .post(url, data)
                .then((response) => {
                  that.loadingKey = false;
                  if (response.code == 0) {
                    that.$message.success("绑定成功");
                    that.dialogclose();
                    that.getListDataFn();
                  } else {
                    that.$message.error(response.msg);
                  }
                })
                .catch(function (error) {
                  that.loadingKey = false;
                  console.log(error);
                });
            })
            .catch(() => {});
        }
      });
    },
    // 重置按钮
    restBnt: function () {
      let that = this;
      let url = "/admin/event/removeFlow";
      let data = {
        event_id: this.detailFormData.id,
      };
      this.$refs.dialogDetailForm.validate((valid) => {
        if (!valid) {
          return false;
        } else {
          that
            .$confirm("确认重置绑定?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
            })
            .then(() => {
              that.loadingText = "处理中...";
              that.loadingKey = true;
              that.axios
                .post(url, data)
                .then((response) => {
                  that.loadingKey = false;
                  if (response.code == 0) {
                    that.$message.success("重置成功");
                    that.dialogclose();
                    that.getListDataFn();
                  } else {
                    that.$message.error(response.msg);
                  }
                })
                .catch(function (error) {
                  that.loadingKey = false;
                  console.log(error);
                });
            })
            .catch(() => {});
        }
      });
    },
    // 关闭弹窗
    dialogclose: function () {
      this.dialogVisible = false;
    },
  },
};
</script>
